package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsService;
import com.dongdongshop.vo.GootsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@DubboService
public class TbGoodsServiceImpl implements TbGoodsService {

    //注入mapper
    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;

    //添加商品(三表添加)
    @Transactional
    @Override
    public void add(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, TbItem tbItem) {
        tbGoodsMapper.insertSelective(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        tbItem.setGoodsId(tbGoods.getId());
        tbItemMapper.insertSelective(tbItem);
    }

    //查询商品信息(要求分页)
    @Override
    public PageResult<GootsVO> TbGoodsshowall(Integer pageNum, Integer pageSize, String goodsName) {
        PageHelper.startPage(pageNum, pageSize);
        List<GootsVO> list = tbGoodsMapper.showbygoodsName(goodsName);
        PageInfo<GootsVO> info = new PageInfo<>(list);
        return new PageResult<GootsVO>(pageNum, pageSize, info.getTotal(), info.getList(), info.getPages());
    }

    //删除单个数据
    @Override
    public void deleteTbGoods(Long id) {
        tbGoodsMapper.deleteByPrimaryKey(id);
    }

    //删除多个数据
    @Override
    public void deleteallTbGoods(List<Long> ids) {
        TbGoodsExample te = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = te.createCriteria();
        criteria.andIdIn(ids);
        tbGoodsMapper.deleteByExample(te);
    }

    //审核通过
    @Override
    public void updateallTbGoodsbya(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(ids.get(i));
            tbGoods.setAuditStatus("1");
            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        }
    }

    //审核驳回
    @Override
    public void updateallTbGoodsbyb(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(ids.get(i));
            tbGoods.setAuditStatus("2");
            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        }
    }

    //根据id查询数据
    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    //根据id查询数据
    @Override
    public TbGoodsDesc getGoodsDescById(Long goodsId) {
        return tbGoodsDescMapper.selectByPrimaryKey(goodsId);
    }

    //调用goods接口查询TbItem数据(根据id查询数据)
    @Override
    public List<TbItem> getTbItemByGoodsId(Long goodsId) {
        TbItemExample te = new TbItemExample();
        TbItemExample.Criteria criteria = te.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        return tbItemMapper.selectByExample(te);
    }
}
